<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\skits;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\app\App;
use app\common\model\skits\Skits;
use app\common\lists\ListsSearchInterface;
use app\common\model\skits\SkitsCategory;


/**
 * Skits列表
 * Class SkitsLists
 * @package app\adminapi\lists
 */
class SkitsLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/13 15:05
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['skits_name', 'producer_name', 'category_id'],
            '='      => ['id', 'status', 'is_finish'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/07/13 15:05
     */
    public function lists(): array
    {
        $appId   = $this->request->get('app_id', 0);
        $channel = App::where('id', $appId)->value('channel') ?? 0;

        $query = Skits::with([
            'actor' => function ($q) {
                $q->withField('id,skits_id,name,photo,role,profile');
            }
        ])->where($this->searchWhere)->where($this->queryWhere());

        if ($channel == 1) {
            $query->where('douyin_drama_id', '<>', '');
        }

        if ($channel == 2) {
            $query->where('wechat_drama_id', '<>', '');
        }

        $list = $query->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();

        foreach ($list as &$item) {
            $names            = SkitsCategory::whereIn('id', explode(',', $item['category_id']))->column('name');
            $item['category'] = implode('，', $names);
        }

        return $list;
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];
        // 创建时间
        if (!empty($this->params['start_time']) && !empty($this->params['end_time'])) {
            $time    = [strtotime($this->params['start_time']), strtotime($this->params['end_time'])];
            $where[] = ['create_time', 'between', $time];
        }

        if (isset($this->params['min_word_count']) && $this->params['min_word_count'] != '' && isset($this->params['max_word_count']) && $this->params['max_word_count'] != '') {
            $word_count = [$this->params['min_word_count'], $this->params['max_word_count']];
            $where[]    = ['episodes_count', 'between', $word_count];
        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/07/13 15:05
     */
    public function count(): int
    {
        $appId   = $this->request->get('app_id', 0);
        $channel = App::where('id', $appId)->value('channel') ?? 0;

        $query = Skits::where($this->searchWhere)->where($this->queryWhere());

        if ($channel == 1) {
            $query->where('douyin_drama_id', '<>', 0);
        }

        if ($channel == 2) {
            $query->where('wechat_drama_id', '<>', 0);
        }

        return $query->count();
    }

}
